package xyz.longicorn.driver.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

public class JWTUtil {

    //这里的token属性配置最好写在配置文件中，这里为了方便直接写成静态属性
    public static final long EXPIRE_TIME = 60 * 60 * 1000;//token到期时间60分钟，毫秒为单位
    public static final long REFRESH_EXPIRE_TIME = 30 * 60;//RefreshToken到期时间为30分钟，秒为单位
    private static final String TOKEN_SECRET = "ljdyaidarking**3nkjnj??";  //密钥盐
    /**
     * 发行人
     */
    private static final String ISSUER = "DK";

    /**
     * 生成token
     *
     * @param account
     * @param currentTime
     * @return
     */
    public static String create(String account, Long currentTime) {

        String token = null;
        try {
            Date expireAt = new Date(currentTime + EXPIRE_TIME);
            token = JWT.create()
                    .withIssuer(ISSUER)
                    .withClaim("account", account)//存放数据
                    .withClaim("currentTime", currentTime)
                    .withExpiresAt(expireAt)//过期时间
                    .sign(Algorithm.HMAC256(TOKEN_SECRET));
        } catch (IllegalArgumentException | JWTCreationException je) {

        }
        return token;
    }


    /**
     * @param : [token]
     * @return : java.lang.Boolean
     * @throws :
     * @Description ：token验证
     * @author : lj
     * @date : 2020-1-31 22:59
     */
    public static Boolean verify(String token) throws Exception {
        try {
            JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(TOKEN_SECRET))
                    .withIssuer(ISSUER).build();//创建token验证器
            DecodedJWT decodedJWT = jwtVerifier.verify(token);
            System.out.println("认证通过：");
            System.out.println("account: " + decodedJWT.getClaim("account").asString());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("过期时间：" + sdf.format(decodedJWT.getExpiresAt()));
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    public static String getAccount(HttpServletRequest request) {
        String token = request.getHeader("token");
        return getAccount(token);
    }

    public static String getAccount(String token) {
        if (token == null) return null;
        try {
            DecodedJWT decodedJWT = JWT.decode(token);
            return decodedJWT.getClaim("account").asString();
        } catch (JWTCreationException e) {
            return null;
        }
    }

    public static Long getCurrentTime(String token) {
        try {
            DecodedJWT decodedJWT = JWT.decode(token);
            return decodedJWT.getClaim("currentTime").asLong();
        } catch (JWTCreationException e) {
            return null;
        }
    }

}
